<?php if (!defined('THINK_PATH')) exit();?><style>
    input.form-control{width:220px;border:1px solid rgb(204, 204, 204);padding:0 10px;height:30px;}
    
 .list-group-item {
    position: relative;
    display: block;
    padding: 6px 15px;
    margin-bottom: -1px;
    background-color: #fff;
    color:#333;
    font-size: 15px;
}
a {
    text-decoration: none;
}
a:hover {
	text-decoration: none;
	}
.list-group-item  select{
	width:65px;margin:0px;height:28px;
}
.controlsa{
	text-align: center;
}
</style>
<link href="/Public/Admin/css/selector/zTreeStyle/zTreeStyle.css" rel="stylesheet">
<link href="/Public/Admin/js/plugins/BusinessSelector/BusinessSelector.css" rel="stylesheet"/>

<ul class="nav nav-tabs" role="tablist">
    <li class="active" role="presentation"><a data-toggle="tab" href="#Process">流程设置</a></li>
    <li role="presentation"><a data-toggle="tab" href="#Attach">附件设置</a></li>
    <li role="presentation"><a data-toggle="tab" href="#KJ">表单设置</a></li>
</ul>
<div class="tab-content">
    <div role="tabpanel" id="Process" class="tab-pane active">
        <form id="AddProcess" class="form-horizontal" method="post" action="<?php echo U('Admin/BusinessProcess/setting');?>">
            <input type="hidden" name="ID" value="<?php echo ($process["ID"]); ?>"/>
            <div class="control-group">
                <label for="Name" class="control-label">流程名称*</label>
                <div class="controls">
                    <input type="text" class="form-control" name="Name" id="Name" value="<?php echo ($process["Name"]); ?>" placeholder="流程名称">
                </div>
            </div>
            <div class="control-group">
                <label for="BPType" class="control-label">流程类型*</label>
                <div class="controls">
                    <select id="BPType" name="BPType" class="form-control">
                        <?php if(is_array($BPType)): foreach($BPType as $key=>$vo): $selected = $process['BPType']==$key?'selected':''; ?>
                            <option <?php echo ($selected); ?> value="<?php echo ($key); ?>"><?php echo ($vo); ?></option><?php endforeach; endif; ?>
                    </select>
                </div>
            </div>
            <div class="control-group">
                <label for="HandleDpt" class="control-label">主办部门</label>
                <div class="controls">
                    <input type="hidden" class="form-control" value="<?php echo ($handleDepartment["ID"]); ?>" name="HandleDptID" id="HandleDptID" placeholder="主办部门">
                    <input style="cursor:pointer;background:url('./Public/Admin/img/Arrow-Icon.png') right center no-repeat" readonly="readonly" type="text" class="form-control selector-icon" value="<?php echo ($handleDepartment["Name"]); ?>" name="HandleDpt" id="HandleDpt" placeholder="主办部门">
                </div>
            </div>
            <div class="control-group">
                <label for="HandleUser" class="control-label">主办人</label>
                <div class="controls">
                    <input type="hidden" class="form-control" value="<?php echo ($handleUser["ID"]); ?>" name="HandleUserID" id="HandleUserID" placeholder="主办人">
                    <input style="cursor:pointer;background:url('./Public/Admin/img/Arrow-Icon.png') right center no-repeat" type="text" class="form-control" value="<?php echo ($handleUser["TrueName"]); ?>" name="HandleUser" id="HandleUser" placeholder="主办人">
                </div>
            </div>
            <div class="control-group">
                <label for="LimitTime" class="control-label">办理时限</label>
                <div class="controls">
                    <input id="LimitTime" name="LimitTime" type="text" value="<?php echo ($process["LimitTime"]); ?>" class="form-control"/> ( / 天)
                </div>
            </div>
            <div class="control-group">
                <!-- div class="controls">
                    <button id="addBtn" type="button" class="btn btn-default">保存</button>
                </div -->
            </div>
        </form>
    </div>
    <div role="tabpanel" id="Attach" class="tab-pane">
        <form id="AddAttachFile" class="form-horizontal" method="post" action="<?php echo U('Admin/BusinessProcess/addFile');?>">
            <table class="table table-bordered">
                <tr><th>业务材料</th><th>是否显示</th><th>是否录入</th><th>操作</th></tr>
                <?php if(is_array($oldAttachList)): foreach($oldAttachList as $key=>$vo): ?><tr data-id="<?php echo ($vo["BALID"]); ?>">
                        <td><?php echo ($vo["Name"]); ?></td>

                        <td><input class="IsShow" type="checkbox" <?=$vo['IsShow']?'checked':''?>/></td>
                        <td>
                            <label>不能录入<input style="vertical-align: text-bottom;margin-left:5px;" <?php if(intval($vo['IsInput']) === 0){echo 'checked';}?> type="radio" name="IsShow-<?php echo ($key); ?>" value="0"/></label>
                            <label>允许录入<input style="vertical-align: text-bottom;margin-left:5px;" <?php if(intval($vo['IsInput']) === 1){echo 'checked';}?> type="radio" name="IsShow-<?php echo ($key); ?>" value="1"/></label>
                            <label>必须录入<input style="vertical-align: text-bottom;margin-left:5px;" <?php if(intval($vo['IsInput']) === 2){echo 'checked';}?> type="radio" name="IsShow-<?php echo ($key); ?>" value="2"/></label>
                        </td>
                        <td><button type="button" class="btn btn-default attachRemove">删除</button> </td>
                    </tr><?php endforeach; endif; ?>
                <tr>
                    <td colspan="3">
                        <select class="form-control">
                            <?php if(is_array($attachList)): foreach($attachList as $key=>$vo): ?><option value="<?php echo ($vo["ID"]); ?>"><?php echo ($vo["Name"]); ?></option><?php endforeach; endif; ?>
                        </select>
                    </td>
                    <td><button type="button" class="btn btn-success attachAdd">增加</button> </td>
                </tr>
            </table>
            <div class="control-group">
                <!-- div>
                    <button id="attachAddBtn" type="button" class="btn btn-default">保存</button>
                </div -->
            </div>
        </form>
    </div>
    
    <div role="tabpanel" id="KJ" class="tab-pane" style="padding-left: 20px;padding-right: 20px;">
   		<button type="button" class="btn btn-success attachAdd" style="float: left;margin-bottom: 10px;" onclick="AddForm()">增加表单</button>
   		<div style="width:100%; height: 320px; ">
   			<div style="width:35%; font-size:15px;float: left; margin-right: 40px;margin-left:20px;border: 1px solid #ddd;float: left;">
   				<b style="width: 100%; text-align: center; font-size: 16px; display: block;padding: 8px;     border-bottom: 1px solid #ddd;">表单列表</b>
   				<div style="overflow-y: auto; height: 250px; " id="fromList">
   				</div>
   			</div>
   			<div style="width:35%;  border: 1px solid #ddd;float: left;">
   				<b style="width: 100%; text-align: center; font-size: 16px; display: block;padding: 8px;     border-bottom: 1px solid #ddd;" id="KJname">表单控件</b>
   				<div style="overflow-y: auto; height: 250px; ">
   					<table width="100%">
   						<tbody id="KJtbody">

   						</tbody>
   					</table>
   				</div>
   			</div>
   		</div>
    </div>

</div>

<div>
	<div class="controlsa">
        <button class="addBtn attachAddBtn btn btn-default" type="button" >保存</button>&nbsp;&nbsp;&nbsp;
        <button id="CloseBtn" type="button" data-dismiss="modal" class="btn btn-default">关闭</button>
    </div>
</div>


<script type="text/javascript" src="/Public/Admin/js/plugins/BusinessSelector/jquery.ztree.all.js"></script>
<script type="text/javascript" src="/Public/Admin/js/plugins/BusinessSelector/BusinessSelector.js"></script>

<script>
	var layerLoad;
	var processID = <?php echo ($process["ID"]); ?>;
    var memberSelector = new MemberSelector('#HandleDpt',{
        title : '选择部门',
        type : 2
    },function(itemList){
        var ids = '';
        var titles = '';
        for(var i = 0; i < itemList.length; i++){
            var id = itemList[i].id;
            var title = itemList[i].title;
            ids += id;
            ids += (i+1 == itemList.length)?'':',';
            titles += title;
            titles += (i+1 == itemList.length)?'':',';
        }
        $('input[name=HandleDptID]').val(ids);
        $('input[name=HandleDpt]').val(titles);
   
        console.log(itemList);
    });
    var memberSelector2 = new MemberSelector('#HandleUser',{
        title : '选择人员',
        type : 1
    },function(itemList){
        var ids = '';
        var titles = '';
        for(var i = 0; i < itemList.length; i++){
            var id = itemList[i].id;
            var title = itemList[i].title;
            ids += id;
            ids += (i+1 == itemList.length)?'':',';
            titles += title;
            titles += (i+1 == itemList.length)?'':',';
        }
        $('input[name=HandleUserID]').val(ids);
        $('input[name=HandleUser]').val(titles);
		
		
		
        console.log(itemList);
    });

    $('#Attach').on('click','.attachRemove',function(){
        $(this).closest('tr').remove();
    });

    $('#Attach').on('click','.attachAdd',function(){
        var index = $('#AddAttachFile').find('tr').length;
        var select = $(this).closest('tr').find('select');
        if(select.val()){
            var tr = '<tr data-id="'+select.val()+'"><td>'+select.find("option:selected").text()+'</td><td><input class="IsShow" type="checkbox" checked="checked"/></td><td> <label>不能录入<input style="vertical-align: text-bottom;margin-left:5px;" type="radio" name="IsShow-'+index+'" value="0"/></label> <label>允许录入<input style="vertical-align: text-bottom;margin-left:5px;" checked="checked" type="radio" name="IsShow-'+index+'" value="1"/></label> <label>必须录入<input style="vertical-align: text-bottom;margin-left:5px;" type="radio" name="IsShow-'+index+'" value="2"/></label> </td><td><button type="button" class="btn btn-default attachRemove">删除</button></td> </tr>';
            var lastTr = $(this).closest('tr');
            $(tr).insertBefore(lastTr);
        }
    });

    //保存附件
    $('button.attachAddBtn').click(function(){
        var dataList = [];
        var BPID = $('input[name=ID]').val();
        $('#AddAttachFile').find('tr').each(function(){
            var id = $(this).data('id');
            if(id){
                var data = {};
                data['BPID'] = $('input[name=ID]').val();
                data['BALID'] = id;
                data['IsInput'] = $(this).find('input[type=radio]:checked').val();//is(':checked');
                data['IsShow'] = $(this).find('input.IsShow').is(':checked');
                dataList.push(data);
            }
        });
        console.log(dataList);

            var url = $('form#AddAttachFile').attr('action');
            $.ajax({
                url : url,
                type : 'post',
                dataType : 'json',
                data : {
                    BPID : BPID,
                    data : dataList
                },
                success : function(data){
                    console.log(data);
                    if(data.status===1){
                       // alert('保存成功');
                    }
                },
                error : function(data){
                    console.log(data);
                }
            });


    });

    //保存流程
    $('button.addBtn').click(function(){
        var data = {};
        data['ID'] = $('input[name=ID]').val();
        data['Name'] = $.trim($('input[name=Name]').val());
        data['BPType'] = $('select[name=BPType]').val();
        data['HandleDptID'] = $('input[name=HandleDptID]').val();
        data['HandleUserID'] = $('input[name=HandleUserID]').val();
        data['LimitTime'] = $('input[name=LimitTime]').val();
        if(!data['Name']){
            alert('请填写流程名称');return;
        }
        console.log(data);
        var url = $('form#AddProcess').attr('action');
        $.ajax({
            url : url,
            type : 'post',
            dataType : 'json',
            data : {
                data : data
            },
            success : function(data){
                console.log(data);
                if(data.status===1){
                    alert('保存成功');
                }
            },
            error : function(data){
                console.log(data);
            }
        });
    });

	//弹窗 表单列表
	function AddForm(){
		layer.open({
			type: 2,
			title: name,
			skin: '', 
			area: ['20%', '65%'], 
			content: "<?php echo U('Admin/Form/fromtree');?>"
		});
	}
	
	//表单控件绑定流程表
	function setBPformRight(formID){
		layerLoad = layer.load(1, {
		  shade: [0.1,'#fff'] 
		});
		if(formID.length <= 0)return;
		var idStr = "";
		for(var i=0;i<formID.length;i++){  
			idStr += formID[i].id+',';
		}
		idStr = idStr.substr(0,idStr.length-1)
		data = {
			"ID" : idStr,
			"processID" : processID
		}
		$.post("<?php echo U('Admin/Form/setBPformRight');?>",data,function(result){
			if(result.status == 1){
				getBPformList();
			}
			layer.close(layerLoad);
		});
	}
	
	//更新表单列表
	function getBPformList(){
		$.post("<?php echo U('Admin/Form/getBPformList');?>",{"processID":processID},function(result){
			if(result){
				var html = "";
				for(var i in result){
					html += "<a href=\"#\" class=\"list-group-item\" onclick=\"getKJlist("+result[i].ID+",'"+result[i].CName+"')\">"+result[i].CName+"</a>";
				}
				$("#fromList").html("")
				$("#fromList").append(html);
				$('#fromList').find('a:first').click();  //显示第一个表单的控件
			}
		});
	}
	getBPformList();
	
	//更新控件列表
	function getKJlist(id,name){
		$("#KJname").text(name+"表单 控件");
		$.post("<?php echo U('Admin/FormCtr/getFormCtrList');?>",{"ID":id,"processID":processID},function(result){
			$("#KJtbody").html("")
			$("#KJtbody").append(result);
			optionsChange(id,name);
		});
	}
	
	function optionsChange(formID,name){
		$("select").change(function(){  
		 	var options = $(this).children("option:selected");
		 	var lastid = $(this).children("option:last").attr("id");
		 	var val = options.val();
		 	var tr = $(this).parent().parent();
		 	var id = $(tr).attr("id");
			$.post("<?php echo U('Admin/BpFormRight/update');?>",{"ID":id,"RightType":val,"BPID":processID,"ControlID":lastid},function(result){
				//alert(result.info);
				if(result.info == "添加成功！"){
					getKJlist(formID,name);
				}
			});	
		 });
	}
	 
	
</script>